#include "GProfiler.h"

namespace GThread
{

	/****************************************************/
	/*					GProfileStats					*/
	/****************************************************/
	GProfileStats::GProfileStats(const char* ProfileName, const GUtil::GTimeStamp& Stamp)
	{
		Name = ProfileName;

		HitCount = 0;
		TotalTime = 0;
		
		ReportTimer.SetReoccur(true);
		ReportTimer.SetEnabled(true);
		ReportTimer.SetInterval(Stamp);
		ReportTimer.Reset();
	}

	GProfileStats::GProfileStats()
	{
		
	}

	/****************************************************/
	/*					GProfilePoint					*/
	/****************************************************/
	GProfilePoint::GProfilePoint(const char* PointName, GProfileStats* Statistics)
	{
		Name = PointName;
		Stats = Statistics;
		Start = GUtil::GTicks::QueryTickCount();
	}

	GProfilePoint::~GProfilePoint()
	{
		GUInt64 ElapsedTicks = UpdateStats();
		ReportStats(ElapsedTicks);
	}

	GUInt64 GProfilePoint::UpdateStats()
	{
		GUInt64 End = GUtil::GTicks::QueryTickCount();
		GUInt64 Elapsed = (End - Start);

		Stats->TotalTime += Elapsed;
		Stats->HitCount++;

		return Elapsed;
	}

	void GProfilePoint::ReportStats(GUInt64 ElapsedTicks)
	{
		if (!Stats->ReportTimer.Chronometer())
		{
			return;
		}

		GUInt32 ThreadId = GThread::GetCurrentThreadId();
		GUInt64 LocalTimeSpent = GUtil::GTimer::GetElapsedTime(ElapsedTicks, Stats->ReportTimer.GetInterval().Ratio);
		GUInt64 GlobalTimeSpent = GUtil::GTimer::GetElapsedTime(Stats->TotalTime, Stats->ReportTimer.GetInterval().Ratio);

		printf_s("TID: %d @ Profile|Point: %s|%s SPENT Local: %llu Global: %llu microsec time.", 
								ThreadId, Stats->Name, Name, LocalTimeSpent, GlobalTimeSpent);
	}

}